(function( $ ){
	function diffDate(time) {
		var one_day = 1000*60*60*24;
		var one_hour = 1000*60*60;
		var one_minute = 1000*60;
		var one_second = 1000;
		
		this.days = 0;
		this.hours = 0;
		this.minutes = 0;
		this.seconds = 0;
		
		if(time > 0) {
			this.days = Math.floor(time/one_day);
			time -= this.days*one_day;
			
			this.hours = Math.floor(time/one_hour);
			time -= this.hours*one_hour;
	
			this.minutes = Math.floor(time/one_minute);
			time -= this.minutes*one_minute;
	
			this.seconds = Math.floor(time/one_second);
			time -= this.seconds*one_second;
		}
	}

	function getTimeAsString(time) {
		var timeString = '';
		if(time.days) timeString += time.days + ' days, ';
		timeString += (time.hours < 10 ? "0" + time.hours : time.hours) + ":";
		timeString += (time.minutes < 10 ? "0" + time.minutes : time.minutes);
		timeString += ' ' + (time.seconds < 10 ? "0" + time.seconds : time.seconds);

		return timeString;
	}
	
	$.fn.timerlist = function( options ) {
		var $this = $(this); // The <ul> element
		$this.data('page', 1);
		var settings = {
				'dataSource': '', // The ajax source of the data
				'data': [],       // The initial data
				'size': 4,       // The number of elements to show in the box
				'intervalTime': 1000, // The interval time to execute the counters update (ms)
				'jsAction':	null,	// Action to execute on each link into the li element. For example fo going to its detail.
		};

		setInterval(function(){
			$this.children("li").each(function(i, li){
				var link = $(li).children("a");
				var span = $(li).children("span")
				var newTime = link.attr('rel')-settings['intervalTime'];
				link.attr('rel', newTime);
				span.html(getTimeAsString(new diffDate(newTime)));
			});
		}, settings['intervalTime']);
		
		var draw = 	function() {
			$this.children().remove(); // Removes all <li> elements
			
			$(settings['data']).each(function(i, data){
				var diff = data['time'] - new Date();
				var time = new diffDate(diff);
				var detailLink = settings['jsAction'] != null ? settings['jsAction']({'id': data['id']}) : "#";
				var li = $("<li>");
				var link = $("<a>", {id: 'link-'+data['id'], href: detailLink, html: data['title'], rel: diff});
				var span = $("<span>", {html: getTimeAsString(time)}).addClass("downtime");
				
				li.append(link);
			    li.append(span);
				$this.append(li);
			}); 	
		};

		var callserver = function() {
			var page = $this.data('page');
			$.get(settings['dataSource'], {size: settings['size'], page: page}, function(data) {
				if(data.length == 0) $this.data('page',1);
				settings['data'] = data;
				draw(settings['data']);
			});
			$this.data('page', page+1);
		}
		
		if( options ) {
			$.extend(settings, options);
		}

		if(settings['dataSource']) {
			callserver();
		} else {
			draw(settings['data']);
		}
		
		$this.parent().children("a").click(function(){
			callserver();
			return false;
		});
		
		
		return $this;
	};	
})( jQuery );
